package com.xul.movie.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xul.movie.domain.Movie;
import com.xul.movie.service.MovieService;

//控制层

@Controller
public class MovieController {

	@Resource
	private MovieService movieService;
	
	@RequestMapping(value = "list")
	public String list(Model model,
			@RequestParam(defaultValue = "",required = false)String mname,
			@RequestParam(defaultValue = "1")Integer cpage,
			@RequestParam(defaultValue = "2")Integer pageSize
			) {
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("mname", mname);
		
		PageHelper.startPage(cpage, pageSize);
		
		List<Movie> list = movieService.findMovieList(map);
		
		PageInfo<Movie> page = new PageInfo<>(list);
		
		model.addAttribute("list", list);
		
		model.addAttribute("page", page);
		
		return "list";
		
	}
	
	@RequestMapping(value = "/toadd")
	public String toadd() {
		
		return "add";
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/add")
	public int add(Movie movie) {
		
		int i = movieService.addMovie(movie);
		
		return i;
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/todelete")
	public int todelete(Movie movie) {
		
		int i = movieService.deleteMovie(movie);
		
		return i;
		
	}
	
	@RequestMapping(value = "/toupdate")
	public String toupdate(Model model,Movie movie) {
		
		Movie mid = movieService.findMovieByMid(movie);
		
		model.addAttribute("mid", mid);
		
		return "update";
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/update")
	public int update(Movie movie) {
		
		int i = movieService.updateMovie(movie);
		
		return i;
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/deleteAll")
	public int deleteAll(int [] ck) {
		
		int i = movieService.deleteAll(ck);
		
		return i;
		
	}
	
	@RequestMapping(value = "/tolook")
	public String tolook(int mid,Model model) {
		
		Movie look = movieService.findMovieLook(mid);
		
		model.addAttribute("look", look);
		
		return "look";
		
		
		
	}
}
